/// Create a new HTTP handle with appropriate global configuration for cargo.
pub fn http_handle(config: &Config) -> CargoResult<http::Handle> {
- Ok(match try!(http_proxy(config)) {
- Some(proxy) => http::handle().proxy(proxy),
- None => http::handle(),
- })
+ let handle = http::handle();
+ let handle = match try!(http_proxy(config)) {
+ Some(proxy) => handle.proxy(proxy),
+ None => handle,
+ };
+ let handle = match try!(http_timeout(config)) {
+ Some(timeout) => handle.timeout(timeout as usize),
+ None => handle,
+ };
+ Ok(handle)
}
/// Find a globally configured HTTP proxy if one is available.
Ok(os::getenv("HTTP_PROXY"))
}
+pub fn http_timeout(config: &Config) -> CargoResult<Option<i64>> {
+ match try!(config.get_i64("http.timeout")) {
+ Some((s, _)) => return Ok(Some(s)),
+ None => {}
+ }
+ Ok(os::getenv("HTTP_TIMEOUT").and_then(|s| s.parse()))
+}
+
pub fn registry_login(config: &Config, token: String) -> CargoResult<()> {
let RegistryConfig { index, token: _ } = try!(registry_configuration(config));
let mut map = HashMap::new();
None => return Ok(None),
}
}
+ CV::Integer(_, ref path) |
CV::String(_, ref path) |
CV::List(_, ref path) |
CV::Boolean(_, ref path) => {
}
}
+ pub fn get_i64(&self, key: &str) -> CargoResult<Option<(i64, Path)>> {
+ match try!(self.get(key)) {
+ Some(CV::Integer(i, path)) => Ok(Some((i, path))),
+ Some(val) => self.expected("integer", key, val),
+ None => Ok(None),
+ }
+ }
+
pub fn expected<T>(&self, ty: &str, key: &str, val: CV) -> CargoResult<T> {
val.expected(ty).map_err(|e| {
human(format!("invalid configuration for key `{}`\n{}", key, e))
#[derive(Eq,PartialEq,Clone,RustcDecodable)]
pub enum ConfigValue {
+ Integer(i64, Path),
String(String, Path),
List(Vec<(String, Path)>, Path),
Table(HashMap<String, ConfigValue>, Path),
impl fmt::Show for ConfigValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
- CV::String(ref string, ref path) => {
- write!(f, "{} (from {})", string, path.display())
- }
+ CV::Integer(i, ref path) => write!(f, "{} (from {:?})", i, path),
+ CV::Boolean(b, ref path) => write!(f, "{} (from {:?})", b, path),
+ CV::String(ref s, ref path) => write!(f, "{} (from {:?})", s, path),
CV::List(ref list, ref path) => {
try!(write!(f, "["));
for (i, &(ref s, ref path)) in list.iter().enumerate() {
write!(f, "] (from {:?})", path)
}
CV::Table(ref table, _) => write!(f, "{:?}", table),
- CV::Boolean(b, ref path) => {
- write!(f, "{} (from {})", b, path.display())
- }
}
}
}
}
CV::Table(ref table, _) => table.encode(s),
CV::Boolean(b, _) => b.encode(s),
+ CV::Integer(i, _) => i.encode(s),
}
}
}
match toml {
toml::Value::String(val) => Ok(CV::String(val, path.clone())),
toml::Value::Boolean(b) => Ok(CV::Boolean(b, path.clone())),
+ toml::Value::Integer(i) => Ok(CV::Integer(i, path.clone())),
toml::Value::Array(val) => {
Ok(CV::List(try!(val.into_iter().map(|toml| {
match toml {
fn merge(&mut self, from: ConfigValue) -> CargoResult<()> {
match (self, from) {
(&mut CV::String(..), CV::String(..)) |
+ (&mut CV::Integer(..), CV::Integer(..)) |
(&mut CV::Boolean(..), CV::Boolean(..)) => {}
(&mut CV::List(ref mut old, _), CV::List(ref mut new, _)) => {
let new = mem::replace(new, Vec::new());
Ok(())
}
+ pub fn i64(&self) -> CargoResult<(i64, &Path)> {
+ match *self {
+ CV::Integer(i, ref p) => Ok((i, p)),
+ _ => self.expected("integer"),
+ }
+ }
+
pub fn string(&self) -> CargoResult<(&str, &Path)> {
match *self {
CV::String(ref s, ref p) => Ok((s.as_slice(), p)),
CV::List(..) => "array",
CV::String(..) => "string",
CV::Boolean(..) => "boolean",
+ CV::Integer(..) => "integer",
}
}
pub fn definition_path(&self) -> &Path {
match *self {
CV::Boolean(_, ref p) |
+ CV::Integer(_, ref p) |
CV::String(_, ref p) |
CV::List(_, ref p) |
CV::Table(_, ref p) => p
match self {
CV::Boolean(s, _) => toml::Value::Boolean(s),
CV::String(s, _) => toml::Value::String(s),
+ CV::Integer(i, _) => toml::Value::Integer(i),
CV::List(l, _) => toml::Value::Array(l
.into_iter()
.map(|(s, _)| toml::Value::String(s))